package com.llkj.qingnianguoxue.course;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.CourseTeacherBannerAdapter;
import com.llkj.qingnianguoxue.adapter.CourseTearcherListAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.bean.CourseTeacherBean;
import com.llkj.qingnianguoxue.bean.LunboBean;
import com.llkj.qingnianguoxue.bean.TeacherListBean;
import com.llkj.qingnianguoxue.home.MasterDetailActivity;
import com.llkj.qingnianguoxue.home.SearchActivity;
import com.llkj.qingnianguoxue.home.SearchResultActivity;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.LunBoJumpUtil;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.ToastUtil;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import java.util.ArrayList;
import java.util.List;

public class CourseTearcherActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener,BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener,View.OnClickListener{
    private ImageView ivBack;
    private TextView tvCancel;//取消
    private EditText etSearch;
    private ConvenientBanner ssvBanner;//轮播图
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView lvTearcherList;//推荐导师列表
    private CourseTearcherListAdapter mTearcherListAdapter;
    private List<CourseTeacherBean> mTeacherData;
    private int totalSize,mPage;
    private TextView mBtnMore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_tearcher);

        initView();
        initListener();

//        loadWaitDialog();
        onRefresh();
    }

    private void initView(){
        RelativeLayout rlTop = findViewById(R.id.rl_top);
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTop,R.color.gray_bar);

        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        ivBack = (ImageView) findViewById(R.id.iv_back);
        tvCancel = (TextView) findViewById(R.id.tv_cancel);
        etSearch = (EditText) findViewById(R.id.et_search);
//        etSearch.setFocusable(false);
        lvTearcherList = (RecyclerView) findViewById(R.id.recyclerView);
        lvTearcherList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mTeacherData = new ArrayList<>();
        mTearcherListAdapter = new CourseTearcherListAdapter(R.layout.item_course_teacher,mTeacherData);
        lvTearcherList.setAdapter(mTearcherListAdapter);

        View headerView = LayoutInflater.from(this).inflate(R.layout.header_course_teacher,null);
        ssvBanner = (ConvenientBanner) headerView.findViewById(R.id.ssw_banner);//轮播图
        ssvBanner.setFocusable(false);
        mBtnMore = (TextView) headerView.findViewById(R.id.tv_more);
        mTearcherListAdapter.addHeaderView(headerView);
    }
    /**
     * 轮播图
     *
     * @param list_image
     */
    private void initBanner(final List<LunboBean> list_image) {
        if (list_image == null) {
            return;
        }
        final CourseTeacherBannerAdapter bannerAdapter = new CourseTeacherBannerAdapter(new CourseTeacherBannerAdapter.ClickBannerListener() {

            @Override
            public void clickBanner(int position) {
                LunboBean lunboBean = list_image.get(position);
                // 5 大师 6 课程
                String type = lunboBean.getType();
                LunBoJumpUtil.jumpTo(CourseTearcherActivity.this,type,lunboBean.getContent_id());
            }
        });

        ssvBanner.setPages(new CBViewHolderCreator<CourseTeacherBannerAdapter>() {
            @Override
            public CourseTeacherBannerAdapter createHolder() {
                return bannerAdapter;
            }
        }, list_image);

    }
    private void initListener(){
        refreshLayout.setOnRefreshListener(this);
        mTearcherListAdapter.setOnLoadMoreListener(this);
        ivBack.setOnClickListener(this);
        tvCancel.setOnClickListener(this);
        mBtnMore.setOnClickListener(this);
//        etSearch.setOnClickListener(this);
        lvTearcherList.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                CourseTeacherBean item = mTeacherData.get(i);
                Bundle bundle = new Bundle();
                bundle.putString("id",item.getId());
                bundle.putString("name",item.getName());
                startActivity(TeacherDetailActivity.class,bundle);
            }
        });
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_SEND) {
                    String word = etSearch.getText().toString();
                    if (!StringUtils.isEmpty(word)) {
                        if (StringUtils.isContaintSymbol(word)) {
                            ToastUtil.makeLongText(CourseTearcherActivity.this, "对不起，请不要输入特殊字符");
                            return true;
                        }
                        Intent intent = new Intent(CourseTearcherActivity.this, SearchResultActivity.class);
                        intent.putExtra("searchWord", word);
                        intent.putExtra("searchCategory","teacherSearch");
                        startActivity(intent);
                    } else {
                        ToastUtil.makeLongText(CourseTearcherActivity.this, "请输入搜索关键字");
                    }
                }
                return true;
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_cancel:
                etSearch.setText("");
                break;
            case R.id.et_search:
//                Intent intent = new Intent(this, SearchResultActivity.class);
//                intent.putExtra("searchWord","");
//                intent.putExtra("searchCategory","courseSearch");
//                startActivity(intent);
//                startActivity(SearchActivity.class);
                break;
            case R.id.tv_more:

                break;
        }
    }

    @Override
    public void onRefresh() {
        mPage=1;
        getCurriculum();
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, mTearcherListAdapter, null, lvTearcherList, totalSize);
    }

    @Override
    public void onLoadMore() {
        mPage++;
        getCurriculum();
    }

    private void getCurriculum(){
        HttpMethod.getCurriculum(this,this, String.valueOf(mPage));
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_CURRICULUM_INDEX) {
            TeacherListBean hotRankingBean = GsonUtil.GsonToBean(data, TeacherListBean.class);
            if (hotRankingBean != null) {
                totalSize = Integer.parseInt(hotRankingBean.getNum());
                if (mPage == 1) {
                    mTeacherData.clear();
                    LoadDataHelper.refreshComplete(mTearcherListAdapter, mTeacherData, refreshLayout, lvTearcherList);
                    initBanner(hotRankingBean.getCarousel());
                } else {
                    mTearcherListAdapter.setEnableLoadMore(true);
                    mTearcherListAdapter.loadMoreComplete();
                }
                mTeacherData.addAll(hotRankingBean.getMaster());
                mTearcherListAdapter.notifyDataSetChanged();
            }
        }
    }
    @Override
    public void onResume() {
        super.onResume();
        ssvBanner.startTurning(5000);
    }

    @Override
    public void onPause() {
        super.onPause();
        ssvBanner.stopTurning();
    }
}
